package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.menggudf.entity.AdminPermissions;
import com.menggudf.entity.Permission;

@Repository
public class PermissionDao {
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(PermissionDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(Permission transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            LOGGER.error("persist failed", re);
            throw re;
        }
    }

    public void remove(Permission persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            LOGGER.error("remove failed", re);
            throw re;
        }
    }

    public Permission merge(Permission detachedInstance) {
        LOGGER.debug("merging Permission instance");
        try {
            return entityManager.merge(detachedInstance);
        } catch (RuntimeException re) {
            LOGGER.error("merge failed", re);
            throw re;
        }
    }

    public Permission findById(Long id) {
        try {
            return entityManager.find(Permission.class, id);
        } catch (RuntimeException re) {
            LOGGER.error("get failed", re);
            throw re;
        }
    }

    public List<Permission> getAllPerssions() {
        try {
            Query query = entityManager.createQuery("from Permission");
            return query.getResultList();
        } catch (Exception e) {
            LOGGER.error("getAllPerssions fail",e);
            throw e;
        }
    }

    public List<Permission> getAllOnePerssions() {
        try {
            Query query = entityManager.createQuery("from Permission where oneModule is null");
            return query.getResultList();
        } catch (Exception e) {
            LOGGER.error("getAllOnePerssions fail",e);
            throw e;
        }
    }

    public List<Permission> getAllTwoPerssions() {
        try {
            Query query = entityManager.createQuery("from Permission where oneModule is not null");
            return query.getResultList();
        } catch (Exception e) {
            LOGGER.error("getAllTwoPerssions fail",e);
            throw e;
        }
    }

    public String getPermissionCode(Integer twoModule) {
        try {
            Query query = entityManager.createQuery("select code from Permission where twoModule=?1").setParameter(1, twoModule);
            List<String> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("getAllTwoPerssions fail",e);
            throw e;
        }
    }

}
